﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Server;
using System.Diagnostics;

namespace DataCenter.Share
{
    public class CenterSetting
    {
        private CenterSetting()
        {
            
        }
        private static CenterSetting Instance = null;
        public static CenterSetting CreateInstance()
        {
            if (Instance == null)
            {
                Instance = new CenterSetting();
                Instance.Initialize();
            }
            return Instance;
        }
        public static CenterSetting GetInstance()
        {
            if (Instance == null)
                Instance = new CenterSetting();
            return Instance;
        }

        private int maxMemoryLimit = 0;
        private string appFilePath = "";
        private string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
        private string dbName = "DataCenter";

        public int MaxMemoryLimit
        {
            get { return maxMemoryLimit; }
            set { maxMemoryLimit = value; }
        }

        public string AppFilePath
        {
            get { return appFilePath; }
            set { appFilePath = value; }
        }

        public string LogFilePath
        {
            get { return logFilePath; }
            set { logFilePath = value; }
        }

        public string DbName
        {
            get { return dbName; }
            set { dbName = value; }
        }

        public void Initialize()
        {
            using (SqlConnection connection = new SqlConnection("context connection = true"))
            {
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    Initialize(command);
                }
            }
        }
        public void Initialize(SqlCommand command)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string commandString = "SELECT * FROM DataCenterSetting";

            command.CommandText = commandString;
            command.CommandType = CommandType.Text;

            using (SqlDataReader reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    int iValue;

                    object oValue = reader["MaxMemoryLimit"];
                    if (oValue != DBNull.Value && !string.IsNullOrEmpty(oValue.ToString()))
                    {
                        if (int.TryParse(oValue.ToString(), out iValue))
                            maxMemoryLimit = iValue;
                    }

                    oValue = reader["AppFilePath"];
                    if (oValue != DBNull.Value && !string.IsNullOrEmpty(oValue.ToString()))
                    {
                        appFilePath = oValue.ToString();
                    }

                    oValue = reader["LogFilePath"];
                    if (oValue != DBNull.Value && !string.IsNullOrEmpty(oValue.ToString()))
                    {
                        logFilePath = oValue.ToString();
                    }
                }
            }

            command.CommandText = "SELECT db_name()";
            command.CommandType = CommandType.Text;
            dbName = command.ExecuteScalar().ToString();

            watch.Stop();
            if (SqlContext.IsAvailable)
                SqlContext.Pipe.Send("CenterSetting: " + watch.ElapsedMilliseconds);
        }
        public void Update(SqlCommand command)
        {
            string commandString = @"UPDATE DataCenterSetting SET MaxMemoryLimit = @MaxMemoryLimit, AppFilePath = @AppFilePath";
            command.CommandText = commandString;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@MaxMemoryLimit", maxMemoryLimit);
            command.Parameters.AddWithValue("@AppFilePath", appFilePath);            
            if (command.ExecuteNonQuery() == 0)
            {
                command.Parameters.Clear();
                Insert(command);
            }            
        }
        public void Insert(SqlCommand command)
        {
            string commandString = @"INSERT INTO DataCenterSetting(MaxMemoryLimit, AppFilePath) VALUES(@MaxMemoryLimit, @AppFilePath)";
            command.CommandText = commandString;
            command.Parameters.AddWithValue("@MaxMemoryLimit", maxMemoryLimit);
            command.Parameters.AddWithValue("@AppFilePath", appFilePath);
            command.ExecuteNonQuery();
            command.Parameters.Clear();
        }
    }
}
